package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import com.keyroy.game.puzzle_rpg.models.functions.Function;
import com.keyroy.game.puzzle_rpg.models.functions.HPFunction;

public class HPFunctionEditor extends Shell {
	private Text text;
	private Spinner value_seed;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			HPFunctionEditor shell = new HPFunctionEditor(display, new HPFunction(), null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public HPFunctionEditor(Display display, HPFunction function, HPFunctionListener listener) {
		this(display, listener);
		value_seed.setSelection(function.hpSeed);
		flushState();
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 * @wbp.parser.constructor
	 */
	public HPFunctionEditor(Display display, final HPFunctionListener listener) {
		super(display, SWT.DIALOG_TRIM);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(10, 10, 61, 17);
		lblNewLabel.setText("\u4EBA\u7269\u4F53\u529B X");

		value_seed = new Spinner(this, SWT.BORDER);
		value_seed.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				flushState();
			}
		});
		value_seed.setMaximum(999999);
		value_seed.setMinimum(-999999);
		value_seed.setBounds(77, 7, 357, 23);

		text = new Text(this, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP);
		text.setBounds(77, 44, 357, 127);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					listener.onCreate(create());
				}
				dispose();
			}
		});
		btnNewButton.setBounds(354, 177, 80, 27);
		btnNewButton.setText("\u5B8C\u6210");

		Label label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setBounds(10, 36, 424, 2);

		Label lblNewLabel_3 = new Label(this, SWT.NONE);
		lblNewLabel_3.setBounds(10, 47, 61, 17);
		lblNewLabel_3.setText("\u63CF\u8FF0 : ");
		createContents();
	}

	private HPFunction create() {
		HPFunction function = new HPFunction();
		function.hpSeed = value_seed.getSelection();
		return function;
	}

	private void flushState() {
		Function function = create();
		text.setText(function.getTypeName() + " : " + function.getDescription());
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("HP\u8BA1\u7B97\u516C\u5F0F");
		setSize(450, 237);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public interface HPFunctionListener {
		public void onCreate(HPFunction function);
	}
}
